﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace TotalSport
{
    public partial class CreateUser : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["TotalSport"].ConnectionString;
            if (!IsPostBack)
            {
                BAL.Customers.PopulateLocation(ref ddlLocation);
                clear();
            }
           
            if (Session["UserId"] == null || Session["UserId"].ToString() == "")
            {
                Session.Abandon();
                Session.RemoveAll();
                Response.Redirect("LoginUser.aspx");
            }
            //int count = ddlLocation.Items.Count;
            
            //ListItem list = new ListItem("Admin", "Admin");
            //ddlLocation.Items.Insert(count, list);
        }

        protected void btnCUser_Click(object sender, EventArgs e)
        {
            string firstname = txtname.Text;
            string lastnam = txtlastname.Text;
            string name = txtUsername.Text;
            string password = txtPassword.Text;
            string mobileno = txtMobileNO.Text;
            string location = ddlLocation.SelectedValue;
            string address = txtAddress.Text;
            string stpas = BAL.Encryption.Encript(password, "&%#@?,:*");
            //int exist = BAL.Customers.verifyuser(name, stpas, location);
         

            int userid;
            userid = BAL.Customers.InsertUserDetails(name, stpas, mobileno, address, location, firstname,lastnam);
                

            if (userid == -1)
            {
                spanMsg.Style.Add("Display", "block");
                spanMsg.InnerHtml = "Username already exist";
            }
            else
            {
                spanMsg.Style.Add("Display", "block");
                spanMsg.InnerHtml = "User created successfully and link sent to Email for Confirmation";
                BAL.Customers.SendUserDetails(firstname,name, password,location);
                BAL.Customers.RegisterLink(name, userid.ToString(), stpas, firstname);
            }
            clear();
        }

        public void clear()
        {
            txtAddress.Text = "";
            txtCPassword.Text = "";
            txtMobileNO.Text = "";
            txtUsername.Text = "";
            txtPassword.Text = "";
            txtlastname.Text = "";
            txtname.Text = "";
            ddlLocation.ClearSelection();
        }
       
    }
}